Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
¿Cómo puedo usar la condición "if" en "whereIn" en laravel?

A continuación se muestra mi consulta sobre cuál usé y funcionó bien para mí.

 if($t_requested['category_id']) { $t_query_condition['p.category_id'] = $t_requested['category_id']; } if($t_requested['brand_id']) { $t_query_condition['p.brand_id'] = $t_requested['brand_id']; } $browseData = DB::table('products as p') ->leftjoin('categories as cp', 'p.category_id', '=', 'cp.id') ->leftjoin('brands as bp', 'p.brand_id', '=', 'bp.id') ->select('p.id as product_id','p.name as product_name','cp.title as category_name','bp.name as brand_name','p.product_weight', 'p.short_description','p.product_price','p.special_price','p.stock_quantity') ->orderBy('p.created_by', "desc") ->offset(0) ->limit(10) ->where($t_query_condition) ->get();

pero ahora tengo múltiples ID en "category_id" y "brand_id", quiero usar whereIn pero se usó con una condición. si obtengo category_id o brand_id nulo, entonces se salta.

gracias de antemano.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Prueba esta consulta, espero que obtengas el resultado.

 $browseData = DB::table('products as p') ->select('p.id as product_id','p.name as product_name','cp.title as category_name','bp.name as brand_name','p.product_weight', 'p.short_description','p.product_price','p.special_price','p.stock_quantity') ->leftjoin('categories as cp', 'p.category_id', '=', 'cp.id') ->leftjoin('brands as bp', 'p.brand_id', '=', 'bp.id') ->orderBy('p.created_by', "desc"); if($t_requested['category_id']) { $browseData->whereIn('p.category_id',$t_requested['category_id']); } if($t_requested['brand_id']) { $browseData->whereIn('p.brand_id',$t_requested['brand_id']); } $result=browseData->offset(0)->limit(10)->get();
over 4 years ago · Santiago Trujillo Report

0

Creo que es muy simple, tienes que pasar una matriz de valores en lugar de un valor

He tratado de escribir opciones para lograr esto, debe seguir cuál es adecuado para usted

 if($t_requested['category_id']) { $t_query_condition['p.category_id'] = $t_requested['category_id']; // it should be array OR $t_query_condition['p.category_id'] = explode(",",$t_requested['category_id']); // if you get value comma saperated $t_query_condition['p.category_id'] = explode(",",$t_requested['category_id']); // if you get value comma saperated $t_query_condition['p.category_id'] = [$t_requested['category_id']]; // if you want to convert one value into array }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!